package cn.com.se.业务工具.绩效OKR基础数据SQL生成;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

// 定义 KeyResult 类
class KeyResult {
    private String content;
    private String weight;

    public KeyResult(String content, String weight) {
        this.content = content;
        this.weight = weight;
    }

    public String getContent() {
        return content;
    }

    public String getWeight() {
        return weight;
    }
}

// 定义 OkrBaseData 类
class OkrBaseData {
    private String person;
    private String department;
    private String period;
    private String objectiveContent;
    private List<KeyResult> keyResults;

    public OkrBaseData(String person, String department, String period, String objectiveContent, List<KeyResult> keyResults) {
        this.person = person;
        this.department = department;
        this.period = period;
        this.objectiveContent = objectiveContent;
        this.keyResults = keyResults;
    }

    public String getPerson() {
        return person;
    }

    public String getDepartment() {
        return department;
    }

    public String getPeriod() {
        return period;
    }

    public String getObjectiveContent() {
        return objectiveContent;
    }

    public List<KeyResult> getKeyResults() {
        return keyResults;
    }
}

// 定义 Objective 类
@Data
@AllArgsConstructor
class Objective {
    private long id;
    private String year;
    private String quarter;
    private String employeeId;
    private String okrId;
    private String okrName;
    private String periodId;
    private long objectiveId;
    private String objectiveContent;
    private double objectiveScore;
    private double objectiveWeight;
    private double sortNo;
    private String createDate;
    private String updateDate;

}

// 定义 Kr 类
@Data
@AllArgsConstructor
class Kr {
    private long id;
    private long objectiveMainId;
    private long krId;
    private String krContent;
    private double krScore;
    private double krWeight;
    private double sortNo;
    private String createDate;
    private String updateDate;

}

public class OkrBaseDataGenerator {
    // 人员基础数据映射
    private static final Map<String, String> EMPLOYEE_MAP = new HashMap<String,String>() {{
        put("景明", "811912463336813698");
    }};

    // OKR主表基础数据映射
    private static final Map<String, String> OKR_ID_MAP = new HashMap<String,String>() {{
        put("景明", "2025032613480000001");
    }};

    // 固定参数
    private static final String PERIOD_ID = "7449050384112615452";
    private static final String OKR_NAME = "2025 年 1 月 - 3 月";

    public static void main(String[] args) {
        List<OkrBaseData> okrBaseDataList = new ArrayList<>();

        // 燃子的 OKR 数据
        okrBaseDataList.add(new OkrBaseData(
                "景明",
                "新零售事业部",
                "O1",
                "实现Revive线下团队的顺利接管，确保无核心成员离职",
                Arrays.asList(
                        new KeyResult("通过与与团队成员1对1谈话+线下全国销售团队会议，增强团队凝聚力，建立团队对水羊管理层及未来生意的信心，确保团队成员0离职", "50%"),
                        new KeyResult("基于接手后的生意评估，输出整体线下团队组织架构，确定招聘岗位", "30%"),
                        new KeyResult("确保两位新总监顺利上岗，确保与前任渠道管理层顺利完成各运营事项培训，并验收培训成功", "20%")
                )
        ));

        okrBaseDataList.add(new OkrBaseData(
                "景明",
                "新零售事业部",
                "O2",
                "实现Revive线下零售商侧顺利接管，确保合作及运营事项无重大风险发生；协助市场团队完成零售商处消费者调研，为生意策略做准备",
                Arrays.asList(
                        new KeyResult("3月内通过完成多场重要零售商高层会议，在零售商处建立良好的初始合作关系,无重大客情风险发生，实现客户关系的顺利接管", "40%"),
                        new KeyResult("梳理完成所有重点零售商的短期紧急事项交接，并留档零售商重要历史文件，确保重大风险0发生", "30%"),
                        new KeyResult("协助市场团队完成至少42名线下用户市场调研，赋能品牌升级", "30%")
                )
        ));

        okrBaseDataList.add(new OkrBaseData(
                "景明",
                "新零售事业部",
                "O3",
                "实现Revive线下运营事项平稳交接，无重大运营风险发生，并归档完成所有重要历史文件",
                Arrays.asList(
                        new KeyResult("确保线下各运营事项的顺利接管，保障线下生意的正常运作，确保无生意因交接产生断档，确保重大运营风险0发生", "40%"),
                        new KeyResult("监督前线下运营负责人离职前， 确保所有重要的历史文件完成分类整理及归档", "30%"),
                        new KeyResult("整理清晰所有财务细项花费，并和财务BP确认单店模型所需的细项全部准备齐全", "30%")
                )
        ));

        // 生成Objective和KR数据
        List<Objective> objectives = new ArrayList<>();
        List<Kr> krs = new ArrayList<>();

        for (OkrBaseData data : okrBaseDataList) {
            String employeeId = EMPLOYEE_MAP.get(data.getPerson());
            String okrId = OKR_ID_MAP.get(data.getPerson());
            String period = data.getPeriod();
            int sortNo = Integer.parseInt(period.replace("O", ""));
            String objectiveContent = escapeSpecialCharacters(data.getObjectiveContent());

            // 生成Objective数据
            long objId = generateId();
            LocalDateTime now = LocalDateTime.now();
            String createDate = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

            Objective objective = new Objective(
                    objId,
                    "2025",
                    "Q1",
                    employeeId,
                    okrId,
                    OKR_NAME,
                    PERIOD_ID,
                    objId,
                    objectiveContent,
                    0.0,
                    100.0,
                    (double) sortNo,
                    createDate,
                    createDate
            );
            objectives.add(objective);

            // 生成KR数据
            int krSort = 1;
            for (KeyResult kr : data.getKeyResults()) {
                long krId = generateId();
                String krContent = escapeSpecialCharacters(kr.getContent());
                double krWeight = Double.parseDouble(kr.getWeight().replace("%", ""));

                Kr krRecord = new Kr(
                        krId,
                        objId,
                        krId,
                        krContent,
                        0.0,
                        krWeight,
                        (double) krSort,
                        createDate,
                        createDate
                );
                krs.add(krRecord);
                krSort++;
            }
        }

        // 输出Objective SQL
        System.out.println("---------- Objective SQL ----------");
        for (Objective obj : objectives) {
            System.out.println("INSERT INTO syounger.kpi_okr_objective " +
                    "(id, `year`, quarter, employee_id, okr_id, okr_name, period_id, objective_id, objective_content, objective_score, objective_weight, sort_no, create_date, create_by, update_date, update_by, del_flag) " +
                    "VALUES (" +
                    obj.getId() + ", " +
                    "'" + obj.getYear() + "', " +
                    "'" + obj.getQuarter() + "', " +
                    obj.getEmployeeId()  + ", " +
                    obj.getOkrId() + ", " +
                    "'" + obj.getOkrName() + "', " +
                    obj.getPeriodId() + ", " +
                    obj.getObjectiveId() + ", " +
                    "'" + obj.getObjectiveContent() + "', " +
                    obj.getObjectiveScore() + ", " +
                    obj.getObjectiveWeight() + ", " +
                    obj.getSortNo() + ", " +
                    "'" + obj.getCreateDate() + "', " +
                    "NULL, " +
                    "'" + obj.getUpdateDate() + "', " +
                    "NULL, " +
                    "'0');");
        }

        // 输出KR SQL
        System.out.println("\n---------- KR SQL ----------");
        for (Kr kr : krs) {
            System.out.println("INSERT INTO syounger.kpi_okr_kr " +
                    "(id, objective_main_id, kr_id, kr_content, kr_score, kr_weight, sort_no, create_date, create_by, update_date, update_by, del_flag) " +
                    "VALUES (" +
                    kr.getId()  + ", " +
                    kr.getObjectiveMainId() + ", " +
                    kr.getKrId() + ", " +
                    "'" + kr.getKrContent() + "', " +
                    kr.getKrScore() + ", " +
                    kr.getKrWeight() + ", " +
                    kr.getSortNo() + ", " +
                    "'" + kr.getCreateDate() + "', " +
                    "NULL, " +
                    "'" + kr.getUpdateDate() + "', " +
                    "NULL, " +
                    "'0');");
        }
    }

    // 生成唯一ID
    private static synchronized long generateId() {
        /*DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        String timestamp = LocalDateTime.now().format(formatter);
        Random random = new Random();
        return timestamp + String.format("%03d", random.nextInt(1000));*/
        return Math.abs(UUID.randomUUID().getMostSignificantBits());
    }

    // 转义特殊字符
    private static String escapeSpecialCharacters(String content) {
        return content.replace("'", "''")
                .replace("\n", "''");
    }
}    